/**
 * Copyright (c) 2004-2006, Martin Fiedler
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 **/
 
#define USE_IEKC64
#define USE_IEKC64_TASK

#ifndef USE_IEKC64 ////////////////////////////////////////////////////////////

#include "h264.h"

void main() {
  frame *f;
  h264_open("");
  for(;;) {
    f=h264_decode_frame(1);
    printf("FRAME DECODED @ %p\n",f);
  }
  h264_close();
}

#else /////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <csl.h>
#include <csl_edma.h>
#include <csl_dat.h>

#include <std.h>
#include <sys.h>
#include <tsk.h>

#include "agl.h"
#include "agl_conv.h"
#include "iekc64.h"

#include "h264.h"


#define OUT_WIDTH  352
#define OUT_HEIGHT 576
#define OUT_RES    RES_VGA_PAL_352X576

#define OUT_Y_SIZE (OUT_WIDTH*OUT_HEIGHT)
#define OUT_C_SIZE (OUT_Y_SIZE/2)


void mem_stat(int segid, char *segname) {
  struct MEM_Stat stat;
  MEM_stat(segid,&stat);
  printf("%s (0x%08X) has %d bytes, %d used, %d free\n",
         segname,segid,stat.size,stat.used,stat.length);
}


void tsk_main() {
  Uint32 CoreClock,BiosClock;
  IEKC64_STATUS status;
  IEKC64_VIDEOOUT out_params;
  Handle out;
  Uint16 *rgb,*screen;
  Uint16 *src,*dest,*dstart;
  frame *f;
  int x,y,sx,sy;
  
  LED_set(BRACKET_RED, ON);
  LED_set(BRACKET_GREEN, OFF);
  
  CoreClock=IEKC64_getCpuClock(&BiosClock);
  printf("C64xx @ %u MHz, DSP/BIOS @ %u MHz\n",CoreClock,BiosClock);

  out_params.Standard=PAL;
  out_params.Resolution=OUT_RES;
  out_params.FrameFormat=RGB565PIXEL;
  out_params.VideoOutSelect=VGA;
  if(!IEKC64_SUCCESS(status=VOUT_open(&out_params,&out))) {
    printf("Cannot open VideoOut, status 0x%08X\n",status);
    return;
  }
  if(!IEKC64_SUCCESS(status=VOUT_start(out))) {
    printf("Cannot start VideoOut, status 0x%08X\n",status);
    return;
  }
  if(!(screen=(Uint16*)MEM_alloc(seg_sdrama,OUT_WIDTH*OUT_HEIGHT*2,4))) {
    printf("Cannot allocate Frame Buffer Memory\n");
    return;
  }
  memset(screen,0,OUT_WIDTH*OUT_HEIGHT*2);
  VOUT_putFrame(out,screen,IEKC64_VIDEO_NO_WAIT);
  LED_set(BRACKET_RED, OFF);

  x=h264_open("");
  sx=H264_WIDTH(x);
  sy=H264_HEIGHT(x);
  if(!(rgb=(Uint16*)MEM_alloc(seg_sdrama,OUT_WIDTH*OUT_HEIGHT*2,4))) {
    printf("Cannot allocate intermediate buffer\n");
    return;
  }
  dstart=&screen[(OUT_HEIGHT/2-sy)*OUT_WIDTH+(OUT_WIDTH-sx)/2];
  
  do {
    LED_set(BRACKET_GREEN, ON);
    f=h264_decode_frame(1); printf("\n");
    LED_set(BRACKET_GREEN, OFF);
    
    CONV_IYUVtoRGB16(CONV_RgbCoef,f->L,f->C[0],f->C[1],rgb,sx,sy);
    src=rgb; dest=dstart;
    for(y=sy; y; --y) {
      memcpy(dest,src,sx*2); dest+=OUT_WIDTH;
      memcpy(dest,src,sx*2); dest+=OUT_WIDTH;
      src+=sx;
    }
    
    VOUT_waitDisplayed(out,IEKC64_VIDEO_WAIT_INFINITE);
    VOUT_putFrame(out,screen,IEKC64_VIDEO_NO_WAIT);
  } while(1);
  h264_close();

  abort();
  //VOUT_stop(out);
}


main() {
  IEKC64_STATUS status;
#ifdef USE_IEKC64_TASK
  TSK_Attrs att;
#endif

  CSL_init();
  mem_stat(seg_isram,"ISRAM");
  mem_stat(seg_sdrama,"SDRAMA");
  mem_stat(seg_sdramb,"SDRAMB");

  printf("Initializing IEKC64 ...\n");
  status=IEKC64_init();
  if(!IEKC64_SUCCESS(status)) {
    printf("Cannot initialize IEKC64, status 0x%08X\n",status);
    abort();
  }
  printf("IEKC64 initialized.\n");

#ifdef USE_IEKC64_TASK
  att=TSK_ATTRS;
  att.priority=4;
  att.stacksize=9024;
  att.stackseg=seg_sdrama;
  TSK_create((Fxn)tsk_main,&att);
#else
  tsk_main();
#endif
}

#endif
